﻿using Common;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Xml.Serialization;

namespace CarPCBATest.Procedure
{
    internal class PcbConfigHelper : NotifyBase
    {
        private static string fileName = "TestConfig.xml";
        public static AllPcbConfig AllPcbConfig { get; set; }

        public static AllPcbConfig CreateSample()
        {
            PcbConfig pcbConfig1 = new PcbConfig()
            {
                SNCheck = true,
                SNLength = 35,
                Name = "A控制板L",
                VisionProjPath = "VisionProjPath",
                BurnConfig = new BurnConfig
                {
                    Paud = 500000,
                    Port = "COM12",

                    SubBurnConfigs = new ObservableCollection<SubBurnConfig>
                       {
                           new SubBurnConfig{
                            BrunFilePath = "BurnPath1",

                               McuAddress =1
                           },
                           new SubBurnConfig{
                             BrunFilePath = "BurnPath2",

                               McuAddress =2
                           }
                       },
                },

                ListLedCurrentConfig = new ObservableCollection<LedCurrentConfig>
                {
                    new LedCurrentConfig()
                    {
                        LedDesc = "STOP",
                        TestVoltage = 14,
                        TestCurrent = 2,
                        LowerLimitCurrent = 231,
                        UpperLimitCurrent = 429,
                        ProcName = "STOP",
                        TestMethodName = "Stop_High",
                        WaterFailLight = true
                    },
                    new LedCurrentConfig()
                    {
                        LedDesc = "TAIL高亮",
                        TestVoltage = 14,
                        TestCurrent = 2,
                        LowerLimitCurrent = 141,
                        UpperLimitCurrent = 262,
                        ProcName = "TAIL_HIGH",
                        TestMethodName = "Stop_Mid",
                        WaterFailLight = true
                    },new LedCurrentConfig
                    {
                        LedDesc = "TAIL",
                        TestVoltage = 14,
                        TestCurrent = 2,
                        LowerLimitCurrent = 542,
                        UpperLimitCurrent = 1006,
                        ProcName = "TAIL",
                        TestMethodName = "Stop_Low",
                         WaterFailLight = true
                    },new LedCurrentConfig
                    {
                        LedDesc = "TURN高亮",
                        TestVoltage = 14,
                        TestCurrent = 2,
                        LowerLimitCurrent = 542,
                        UpperLimitCurrent = 1006,
                        ProcName = "TURN_HIGH",
                        TestMethodName = "TurnLeft_High",
                        WaterFailLight = true
                    },new LedCurrentConfig
                    {
                        LedDesc = "TURN",
                        TestVoltage = 14,
                        TestCurrent = 2,
                        LowerLimitCurrent = 542,
                        UpperLimitCurrent = 1006,
                        ProcName = "TURN",
                        TestMethodName = "TurnLeft_Low",
                        WaterFailLight = true
                    }
                }
                ,
                No = 0,
                ListPowerConfig = new ObservableCollection<PowerConfig>()
                 {
                     new PowerConfig ()
                     {
                         ChannelIndex = 1,
                          IsActive = true,
                           Current = 2,
                            Voltage= 13.5,
                     },
                     new PowerConfig ()
                     {
                         ChannelIndex = 2,
                          IsActive = true,
                           Current = 2,
                            Voltage= 13.5,
                     },
                     new PowerConfig ()
                     {
                         ChannelIndex = 3,
                          IsActive = true,
                           Current = 2,
                            Voltage= 13.5,
                     }
                 }
            };
            PcbConfig pcbConfig2 = new PcbConfig()
            {
                SNCheck = true,
                SNLength = 35,
                Name = "A控制板R",
                VisionProjPath = "VisionProjPath",
                BurnConfig = new BurnConfig
                {
                    Paud = 500000,
                    Port = "COM12",

                    SubBurnConfigs = new ObservableCollection<SubBurnConfig>
                       {
                           new SubBurnConfig{
                            BrunFilePath = "BurnPath1",

                               McuAddress =1
                           },
                           new SubBurnConfig{
                             BrunFilePath = "BurnPath2",

                               McuAddress =2
                           }
                       },
                },

                ListLedCurrentConfig = new ObservableCollection<LedCurrentConfig>
                {
                    new LedCurrentConfig()
                    {
                        LedDesc = "STOP",
                        TestVoltage = 14,
                        TestCurrent = 2,
                        LowerLimitCurrent = 231,
                        UpperLimitCurrent = 429,
                        ProcName = "STOP",
                        TestMethodName = "Stop_High",
                        WaterFailLight = true
                    },
                    new LedCurrentConfig()
                    {
                        LedDesc = "TAIL高亮",
                        TestVoltage = 14,
                        TestCurrent = 2,
                        LowerLimitCurrent = 141,
                        UpperLimitCurrent = 262,
                        ProcName = "TAIL_HIGH",
                        TestMethodName = "Stop_Mid",
                        WaterFailLight = true
                    },new LedCurrentConfig
                    {
                        LedDesc = "TAIL",
                        TestVoltage = 14,
                        TestCurrent = 2,
                        LowerLimitCurrent = 542,
                        UpperLimitCurrent = 1006,
                        ProcName = "TAIL",
                        TestMethodName = "Stop_Low",
                         WaterFailLight = true
                    },new LedCurrentConfig
                    {
                        LedDesc = "TURN高亮",
                        TestVoltage = 14,
                        TestCurrent = 2,
                        LowerLimitCurrent = 542,
                        UpperLimitCurrent = 1006,
                        ProcName = "TURN_HIGH",
                        TestMethodName = "TurnLeft_High",
                        WaterFailLight = true
                    },new LedCurrentConfig
                    {
                        LedDesc = "TURN",
                        TestVoltage = 14,
                        TestCurrent = 2,
                        LowerLimitCurrent = 542,
                        UpperLimitCurrent = 1006,
                        ProcName = "TURN",
                        TestMethodName = "TurnLeft_Low",
                        WaterFailLight = true
                    }
                }
                ,
                No = 0,
                ListPowerConfig = new ObservableCollection<PowerConfig>()
                 {
                     new PowerConfig ()
                     {
                         ChannelIndex = 1,
                          IsActive = true,
                           Current = 2,
                            Voltage= 13.5,
                     },
                     new PowerConfig ()
                     {
                         ChannelIndex = 2,
                          IsActive = true,
                           Current = 2,
                            Voltage= 13.5,
                     },
                     new PowerConfig ()
                     {
                         ChannelIndex = 3,
                          IsActive = true,
                           Current = 2,
                            Voltage= 13.5,
                     }
                 }
            };
            //PcbConfig pcbConfig3 = new PcbConfig()
            //{
            //    Name = "C控制板L",
            //    VisionProjPath = "VisionProjPath",
            //    BurnConfig = new BurnConfig
            //    {
            //        Paud = 500000,
            //        Port = "COM4",

            //        SubBurnConfigs = new ObservableCollection<SubBurnConfig>
            //           {
            //               new SubBurnConfig{
            //                BrunFilePath = "BurnPath1",

            //                   McuAddress =1
            //               },
            //               new SubBurnConfig{
            //                 BrunFilePath = "BurnPath2",

            //                   McuAddress =2
            //               }
            //           },
            //    },

            //    ListLedCurrentConfig = new ObservableCollection<LedCurrentConfig>
            //    {
            //        new LedCurrentConfig()
            //        {
            //            LedDesc = "SL",
            //            TestVoltage = 14,
            //            TestCurrent = 2,
            //            LowerLimitCurrent = 231,
            //            UpperLimitCurrent = 429,
            //             ProcName = "SL Vision Test",
            //             WaterFailLight = true
            //        },
            //        new LedCurrentConfig()
            //        {
            //            LedDesc = "TL",
            //            TestVoltage = 14,
            //            TestCurrent = 2,
            //            LowerLimitCurrent = 231,
            //            UpperLimitCurrent = 429,
            //            ProcName = "TL Vision Test",
            //             WaterFailLight = true
            //        },new LedCurrentConfig
            //        {
            //            LedDesc = "TL+GATE",
            //            TestVoltage = 14,
            //            TestCurrent = 2,
            //            LowerLimitCurrent = 231,
            //            UpperLimitCurrent = 429,
            //            ProcName = "TL + GATE Vision Test",
            //             WaterFailLight = true
            //        },
            //        new LedCurrentConfig
            //        {
            //            LedDesc = "RTSL",
            //            TestVoltage = 14,
            //            TestCurrent = 2,
            //            LowerLimitCurrent = 231,
            //            UpperLimitCurrent = 429,
            //             ProcName = "RTSL Vision Test",
            //             WaterFailLight = true
            //        },
            //        new LedCurrentConfig
            //        {
            //            LedDesc = "RTSL+GATE",
            //            TestVoltage = 14,
            //            TestCurrent = 2,
            //            LowerLimitCurrent = 231,
            //            UpperLimitCurrent = 429,
            //             ProcName = "RTSL+GATE Vision Test",
            //             WaterFailLight = true
            //        }
            //    }
            //    ,
            //    No = 2,
            //    ListPowerConfig = new ObservableCollection<PowerConfig>()
            //     {
            //         new PowerConfig ()
            //         {
            //             ChannelIndex = 1,
            //              IsActive = true,
            //               Current = 2,
            //                Voltage= 13.5,
            //         },
            //         new PowerConfig ()
            //         {
            //             ChannelIndex = 2,
            //              IsActive = true,
            //               Current = 2,
            //                Voltage= 13.5,
            //         },
            //         new PowerConfig ()
            //         {
            //             ChannelIndex = 3,
            //              IsActive = true,
            //               Current = 2,
            //                Voltage= 13.5,
            //         }
            //     }
            //};
            PcbConfig confi2 = new PcbConfig();
            //if (File.Exists(fileName))
            //{
            //    return null;
            //}
            AllPcbConfig config = new AllPcbConfig();
            config.DM3025EPort = "COM10";
            config.zhiConPort = "COM9";
            config.IT6302APort = "COM2";
            config.IOModule1IP = "192.168.1.12";
            config.Title = "TITLE";
            config.SN1code_L = "P00212066";
            config.SN2code_L = "P00212070";
            config.SN3code_L = "P00212070";
            config.SN4code_L = "P00212075";

            config.SN1code_R = "P00212065";
            config.SN2code_R = "P00212069";
            config.SN3code_R = "P00212069";
            config.SN4code_R = "P00212073";

            config.JlinkSL = "D:\\Burn\\MERGE-RCLA.jflash";
            config.JlinkEXE = "D:\\Program Files\\JLINK_V72\\JFlash.exe";
            config.IsOneProduct = true;
            config.ListConfig.Add(pcbConfig1);
            config.ListConfig.Add(pcbConfig2);
            //config.ListConfig.Add(pcbConfig3);
            XmlSerializer serializer = new XmlSerializer(typeof(AllPcbConfig));
            TextWriter writer = new StreamWriter(fileName);
            // 初始化默认值

            serializer.Serialize(writer, config);
            writer.Close();
            return config;
        }

        public static PcbConfig GetTestConfig()
        {
            var config = Load();
            return config.ListConfig.Where(t => t.No == config.UsingNo).ToList()[0];
        }

        public static AllPcbConfig Load()
        {
            if (!File.Exists(fileName))
            {
                return CreateSample();
            }
            XmlSerializer serializer = new XmlSerializer(typeof(AllPcbConfig));

            TextReader reader = new StreamReader(fileName);
            // 初始化默认值

            var obj = serializer.Deserialize(reader);
            reader.Close();
            AllPcbConfig = obj as AllPcbConfig;
            return obj as AllPcbConfig;
        }

        public static void Save(AllPcbConfig config)
        {
            AllPcbConfig = config;
            XmlSerializer serializer = new XmlSerializer(typeof(AllPcbConfig));
            TextWriter writer = new StreamWriter(fileName);
            // 初始化默认值

            serializer.Serialize(writer, config);
            writer.Close();
        }

        public static void Save()
        {
            var config = AllPcbConfig;
            XmlSerializer serializer = new XmlSerializer(typeof(AllPcbConfig));
            TextWriter writer = new StreamWriter(fileName);
            // 初始化默认值

            serializer.Serialize(writer, config);
            writer.Close();
        }
    }
}